package org.jshand.jxc.business.book.panel;
import java.awt.Component;  
import java.awt.FlowLayout;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.EventObject;  
  
import javax.swing.AbstractCellEditor;  
import javax.swing.JButton;  
import javax.swing.JCheckBox;  
import javax.swing.JPanel;  
import javax.swing.JTable;  
import javax.swing.UIManager;  
import javax.swing.table.DefaultTableModel;  
import javax.swing.table.TableCellEditor;  
import javax.swing.table.TableCellRenderer;  
import javax.swing.table.TableColumn;  
  
  
public class ButtonColumn extends AbstractCellEditor implements  
        TableCellEditor, TableCellRenderer ,ActionListener{  
  
    private JButton renderButton;  
    private JButton editorButton;  
    private DefaultTableModel dtm;  
    private int row;  
      
    public ButtonColumn(TableColumn tc)  
    {  
        renderButton = new JButton();  
        editorButton = new JButton();  
        editorButton.setFocusable(false);  
        editorButton.addActionListener(this);  
        tc.setCellEditor(this);  
        tc.setCellRenderer(this);  
    }  
      
    public Component getTableCellEditorComponent(JTable table,  
            Object value, boolean isSelected, int row, int column) {  
        // TODO 当单元格处于编辑状态时  
        editorButton.setText("删除");  
        dtm = (DefaultTableModel)table.getModel();  
        this.row = row;  
        return editorButton;  
    }  
  
    @Override  
    public Object getCellEditorValue() {  
        // TODO 当单元格的编辑状态结束后调用此方法内的代码  
        return null;  
    }  
  
    @Override  
    public boolean shouldSelectCell(EventObject arg0) {  
        // TODO Auto-generated method stub  
        return super.shouldSelectCell(arg0);  
    }  
  
    @Override  
    public Component getTableCellRendererComponent(JTable table,  
            Object value, boolean isSelected, boolean hasFocus, int row,  
            int column) {  
        // TODO 当单元格处于展示状态时  
        renderButton.setText("删除");  
        renderButton.setBackground(hasFocus?UIManager.getColor("Button.background"):table.getSelectionBackground());  
        return renderButton;  
    }  
  
    @Override  
    public void actionPerformed(ActionEvent arg0) {  
        this.fireEditingStopped();//立即终止此单元格的编辑状态，使表格模型可以自动更新  
        dtm.removeRow(this.row);  
    }  
  
}  